#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* pruneTree(TreeNode* root)
    {
        bool ret = _pruneTree(root);
        return ret == true ? nullptr : root;
    }

private:
    bool _pruneTree(TreeNode* cur)
    {
        if (cur == nullptr) {
            return true;
        }

        bool leftPruneTree = _pruneTree(cur->left);
        if (leftPruneTree) cur->left = nullptr;

        bool rightPruneTree = _pruneTree(cur->right);
        if (rightPruneTree) cur->right = nullptr;

        return leftPruneTree && rightPruneTree && cur->val == 0;
    }
};